package org.lq.his.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.lq.his.dao.RegistrationDao;
import org.lq.his.dao.SeeDoctorRegisterDao;
import org.lq.his.pojo.Registration;
import org.lq.his.pojo.SeeDoctorRegister;
import org.lq.his.service.SeeDoctorRegisterService;
import org.lq.his.utils.LqFormatDateUtils;
import org.springframework.stereotype.Service;

@Service("seeDoctorRegisterService")
public class SeeDoctorRegisterServiceImpl implements SeeDoctorRegisterService {
	@Resource
	private SeeDoctorRegisterDao seeDoctorRegisterDao;
	
	@Resource
	private RegistrationDao registrationDao;
	
	
	
	public void setRegistrationDao(RegistrationDao registrationDao) {
		this.registrationDao = registrationDao;
	}

	public void setSeeDoctorRegisterDao(SeeDoctorRegisterDao seeDoctorRegisterDao) {
		this.seeDoctorRegisterDao = seeDoctorRegisterDao;
	}
	
	/**
	 * 就诊登记
	 */
	@Override
	public String add(SeeDoctorRegister seeDoctorRegister) {
		//格式化日期
		LqFormatDateUtils lqFormatDateUtils =  new LqFormatDateUtils();
		Date data = lqFormatDateUtils.getTodayDate();
		seeDoctorRegister.setRegisterDate(data);
		//查询是否已经登记了，
		Registration registration = registrationDao.getById(seeDoctorRegister.getRegistration().getId());
		
		if(registration==null||registration.getId()<1) {
			return "该挂号记录已不存在";
		}else if(registration.getRegisterState()==1) {
			return "该号已经登记了，无法再次登记！";
		}
		//修改挂号表的状态为已登记
		registrationDao.updateRegisterState(seeDoctorRegister.getRegistration().getId());
		//设置医生，挂号类型，科室编号
		seeDoctorRegister.setPatient(registration.getPatient());
		seeDoctorRegister.setSection(registration.getSection());
		seeDoctorRegister.setType(registration.getType());
		seeDoctorRegister.setUser(registration.getUser());
		seeDoctorRegisterDao.add(seeDoctorRegister);
		return "登记成功";
	}

	/**
	 * 条件查询已登记病人
	 */
	@Override
	public List<SeeDoctorRegister> getListByCondition(int start, int limit, SeeDoctorRegister condition, String column,
			String orderBy) {
		//格式化今天日期，赋值
		LqFormatDateUtils lqFormatDateUtils = new LqFormatDateUtils();
		Date date = lqFormatDateUtils.getTodayDate();
		condition.setRegisterDate(date);
		
		
		return seeDoctorRegisterDao.getListByCondition(start, limit, condition, column, orderBy);
	}

	/**
	 * 根据条件查询条数
	 */
	@Override
	public Integer getCountByCondition(SeeDoctorRegister condition) {
		return seeDoctorRegisterDao.getCountByCondition(condition);
	}
	
}
